The art of creating W3c Xml Schema

An Xml Schema is a Language written in Xml to define the structure and the grammar of an Xml document. It’s is important to do that so we will have our document validated. In this article We will see the principal elements of the Xml Schema language and an example of xml schema creation.

1. Xml Schema elements

xmlSchemaelements 300x163 The art of creating W3c Xml Schema
We have two categories of elements: The simple type and the complex type. The simple type is compose of Elements , attributes and restrictions.

An element define an object of an Xml file. This element can contains attributes. Elements has an attribute named type. The list of data type can be found in schema references. (Example: String , date, numeric …)
A restriction is a sub part of an element or attribute.

Now let’s talk about the complex type. Complex type is a combination of some or all simple types elements. There are a lot of properties to define well every kind of elements. For example, we have minOccurs and maxOccurs that define his number of occurrence. For more detail you can read this page.

2. Xml Schema Example

Step 1: the Xml document

We have to see the Xml document we are writing the language for.

xmldocument 300x85 The art of creating W3c Xml Schema

Observing the image, we notice we have 4 elements(cluster,job,jobGroup and host) and every element have many attributes.
We also notice the reference to the schema file.

Step 2: Write the first element of the schema

xsbfirsrelt 300x50 The art of creating W3c Xml Schema

You can see the first element is the schema. To construct our schema we will use a pattern named Venetian Blind.
In this pattern, we first design all the simple types and after all the complex types. At the end we define the root element.

venetianBlindPattern 300x200 The art of creating W3c Xml Schema

Step 3: Simple types elements

We have enumeration elements, restriction, string and integer elements.

<!-- definition of simple types -->
  <xs:simpleType name="idType">
    <xs:restriction base="xs:string">
      <xs:pattern value="[a-zA-Z][a-zA-Z0-9]*"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="statusType">
     <xs:restriction base="xs:NMTOKEN">
          <xs:enumeration value="OK"/>
          <xs:enumeration value="UNAVAIL"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="statusOfHostType">
   <xs:restriction base="xs:string">
    <xs:enumeration value="OK"/>
    <xs:enumeration value="UNAVAIL"/>
	<xs:enumeration value="CLOSED"/>
    <xs:enumeration value="UNLICENSED"/>
   </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="typeOfHostType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="Client"/>
    <xs:enumeration value="Server"/>
	<xs:enumeration value="Master"/>
	</xs:restriction>
  </xs:simpleType>
   <xs:simpleType name="statusOfJobType">
   <xs:restriction base="xs:string">
    <xs:enumeration value="PENDING"/>
    <xs:enumeration value="RUNNING"/>
	<xs:enumeration value="DONE"/>
    <xs:enumeration value="EXIT"/>
	<xs:enumeration value="SUSPENDED"/>
	</xs:restriction>
   </xs:simpleType>
  <xs:simpleType name="intType">
    <xs:restriction base="xs:int">
      <!-- integer-->
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="descType">
    <xs:restriction base="xs:string"/>
  </xs:simpleType>
 <xs:simpleType name="dateTimeType">
  <xs:restriction base="xs:string"> 
    <xs:pattern value="[A-Z]{1}[a-z]{2} [0-3]{1}[0-9]{1} [0-2]{1}[0-9]{1}:[0-6]{1}[0-9]{1}"/>
  </xs:restriction>
 </xs:simpleType>

Step 4: complex types elements

For the complex type we define element with simple type element.

<!-- definition of complex types -->
 <xs:complexType name="jobType">
      <xs:attribute name="id" type="intType"/>
      <xs:attribute name="state" type="statusOfJobType"/>
      <xs:attribute name="submissionHost" type="idType"/>
      <xs:attribute name="submissionTime" type="dateTimeType"/>
	  <xs:attribute name="jobGroup" type="idType"/>
	  <xs:attribute name="executionHost" type="idType"/>
  </xs:complexType>
  <xs:complexType name="jobGroupType">  
		<xs:sequence>
    		<xs:element name="description" type="descType" minOccurs="1" maxOccurs="1" />
    	</xs:sequence>
      <xs:attribute name="id" type="idType"/>
  </xs:complexType>
  <xs:complexType name="hostType">
      <xs:attribute name="id" type="idType"/>
      <xs:attribute name="type" type="typeOfHostType"/>
	  <xs:attribute name="status" type="statusOfHostType"/>
	  <xs:attribute name="load" type="intType"/>
	  <xs:attribute name="memory" type="intType"/>
  </xs:complexType>
<xs:complexType name="clusterType">
    <xs:choice minOccurs="1" maxOccurs="unbounded">      
      <xs:element name="host" type="hostType" />
	  <xs:element name="jobGroup" type="jobGroupType" />
	  <xs:element name="job" type="jobType" />
    </xs:choice>
	<xs:attribute name="id" type="idType"/>
	<xs:attribute name="status" type="statusType"/>
	<xs:attribute name="master" type="idType"/>
</xs:complexType>

So at the end we define the root element cluster.
After create your xsd file you want to make sure it is valid. Here you will find an online tool to validate your schema language: validation tool

xml xsd validation tool 300x131 The art of creating W3c Xml Schema

Now you can download the files of this article to see the final result.

download xnl 300x96 The art of creating W3c Xml Schema

For further knowledge go to the xsd element reference.


 The art of creating W3c Xml Schema The art of creating W3c Xml Schema

Precedente How to resolve a Linear Programming Problem using Simplex Algorithm Successivo How to do Port Scanning with Nmap?

Lascia un commento

This site uses Akismet to reduce spam. Learn how your comment data is processed.